package com.prp.algorithm;

import java.util.ArrayList;
import java.util.Collections;

import com.prp.bo.SNSConstants;
import com.prp.db.DAOFactory;
import com.prp.db.idao.IRecommendationEntityDAO;
import com.prp.db.idao.ISubjectCategoryDAO;
import com.prp.db.idao.ISubjectChildCategoryDAO;
import com.prp.db.idao.ISubjectRecommendationDAO;
import com.prp.db.po.RecommendationEntity;
import com.prp.db.po.SubjectCategory;
import com.prp.db.po.SubjectChildCategory;
import com.prp.db.po.SubjectRecommendation;

public class SubjectsRecommend {

	/**
	 * 获取主题帖类别的TOPN推荐
	 * @return
	 */
	public synchronized static ArrayList<SubjectCategory> getSubjectCategoryRecommend(int topNum) {
		DAOFactory factory = DAOFactory.getDAOFactory();
		ISubjectCategoryDAO subcDAO = factory.getSubjectCategoryDAO();
		ISubjectChildCategoryDAO subccDAO = factory
				.getSubjectChildCategoryDAO();
		ISubjectRecommendationDAO subRecDAO = factory
				.getSubjectRecommendationDAO();
		ArrayList<SubjectCategory> subcList = (ArrayList<SubjectCategory>) subcDAO
				.findAll();
		ArrayList<RecommendationRelation> rrList = new ArrayList<RecommendationRelation>();
		
		for (int cIndex = 0; cIndex < subcList.size(); cIndex++) {
			RecommendationRelation tempRelation = new RecommendationRelation();
			tempRelation.index = cIndex;
			ArrayList<SubjectChildCategory> subccList = (ArrayList<SubjectChildCategory>) subccDAO
					.findByCategoryId(subcList.get(cIndex).getCategroyId());
			for (int ccIndex = 0; ccIndex < subccList.size(); ccIndex++) {
				ArrayList<SubjectRecommendation> subRecList = (ArrayList<SubjectRecommendation>) subRecDAO
						.findByCategoryChildId(subccList.get(ccIndex)
								.getChildCategoryId());
				for (int recIndex = 0; recIndex < subRecList.size(); recIndex++) {
					tempRelation.clickValue += subRecList.get(recIndex)
							.getClickFreq();
					tempRelation.waitingValue += subRecList.get(recIndex)
							.getWaitingTime();
					tempRelation.replyValue += subRecList.get(recIndex)
							.getReplyNum();
				}
			}
			rrList.add(tempRelation);
		}

		//进行数据结构的转换，下面开始排序
		rrList = getSortRelation(rrList);

		ArrayList<SubjectCategory> resultList = new ArrayList<SubjectCategory>();
		for (int i = 0; i < rrList.size()
				&& i < topNum; i++)
			resultList.add(subcList.get(rrList.get(i).index));
		return resultList;
	}
	public static void main(String[] args){
		System.out.println("////////////////////////////");
		ArrayList<SubjectCategory> list = getSubjectCategoryRecommend(5);
		
		for( int i = 0; i < list.size(); i++){
			System.out.println(i+ " : "+list.get(i).getCategroyId());
		}
		
		System.out.println("////////////////////////////");
		ArrayList<SubjectChildCategory> list1 = getSubjectChildCategoryRecommend(10);
		
		for( int i = 0; i < list1.size(); i++){
			System.out.println(i+ " : "+list1.get(i).getChildCategoryId());
		}
		
		System.out.println("////////////////////////////");
		ArrayList<SubjectRecommendation> list2 = getSubjectRecommendationRecommend(10);
		
		for( int i = 0; i < list2.size(); i++){
			System.out.println(i+ " : "+list2.get(i).getRecommendationId());
		}
		
		System.out.println("////////////////////////////");
		ArrayList<RecommendationEntity> list3 = getRecommendationEntityRecommend(10);
		
		for( int i = 0; i < list3.size(); i++){
			System.out.println(i+ " : "+list3.get(i).getEntityId());
		}
	}

	/**
	 * 获取主题帖子类别TOPN推荐
	 * @return
	 */
	public synchronized static ArrayList<SubjectChildCategory> getSubjectChildCategoryRecommend(int topNum) {
		DAOFactory factory = DAOFactory.getDAOFactory();
		ISubjectChildCategoryDAO subccDAO = factory
				.getSubjectChildCategoryDAO();
		ISubjectRecommendationDAO subRecDAO = factory
				.getSubjectRecommendationDAO();
		ArrayList<SubjectChildCategory> subccList = (ArrayList<SubjectChildCategory>) subccDAO
				.findAll();
		ArrayList<RecommendationRelation> rrList = new ArrayList<RecommendationRelation>();
		for (int ccIndex = 0; ccIndex < subccList.size(); ccIndex++) {
			RecommendationRelation tempRelation = new RecommendationRelation();
			ArrayList<SubjectRecommendation> subRecList = (ArrayList<SubjectRecommendation>) subRecDAO
					.findByCategoryChildId(subccList.get(ccIndex)
							.getChildCategoryId());
			for (int recIndex = 0; recIndex < subRecList.size(); recIndex++) {
				tempRelation.clickValue += subRecList.get(recIndex)
						.getClickFreq();
				tempRelation.waitingValue += subRecList.get(recIndex)
						.getWaitingTime();
				tempRelation.replyValue += subRecList.get(recIndex)
						.getReplyNum();
				
			}
			rrList.add(tempRelation);
		}

		rrList = getSortRelation(rrList);

		ArrayList<SubjectChildCategory> resultList = new ArrayList<SubjectChildCategory>();
		for (int i = 0; i < rrList.size()
				&& i < topNum; i++){
			resultList.add(subccList.get(rrList.get(i).index));
		}
		return resultList;
	}

	/**
	 * 获取主题帖TOPN推荐
	 * @return
	 */
	public synchronized static ArrayList<SubjectRecommendation> getSubjectRecommendationRecommend(int topNum) {
		DAOFactory factory = DAOFactory.getDAOFactory();
		ISubjectRecommendationDAO subRecDAO = factory
				.getSubjectRecommendationDAO();
		ArrayList<SubjectRecommendation> subRecList = (ArrayList<SubjectRecommendation>) subRecDAO
				.findAll();
		ArrayList<RecommendationRelation> rrList = new ArrayList<RecommendationRelation>();
		for (int i = 0; i < subRecList.size(); i++) {
			RecommendationRelation tempRelation = new RecommendationRelation();
			tempRelation.index = i;
			tempRelation.clickValue = subRecList.get(i).getClickFreq();
			tempRelation.waitingValue = subRecList.get(i).getWaitingTime();
			tempRelation.replyValue = subRecList.get(i).getReplyNum();
			rrList.add(tempRelation);
		}

		rrList = getSortRelation(rrList);

		ArrayList<SubjectRecommendation> resultList = new ArrayList<SubjectRecommendation>();
		for (int i = 0; i < rrList.size()
				&& i < topNum; i++)
			resultList.add(subRecList.get(rrList.get(i).index));
		return resultList;
	}

	/**
	 * 获取推荐实体TOPN推荐
	 * @return
	 */
	public synchronized static ArrayList<RecommendationEntity> getRecommendationEntityRecommend(int topNum) {
		DAOFactory factory = DAOFactory.getDAOFactory();
		IRecommendationEntityDAO reDAO = factory.getRecommendationEntityDAO();
		ArrayList<RecommendationEntity> reList = (ArrayList<RecommendationEntity>) reDAO
				.findAll();
		ArrayList<RecommendationRelation> rrList = new ArrayList<RecommendationRelation>();
		for (int i = 0; i < reList.size(); i++) {
			RecommendationRelation tempRelation = new RecommendationRelation();
			tempRelation.index = i;
			tempRelation.clickValue = reList.get(i).getClickFreq();
			tempRelation.waitingValue = reList.get(i).getWaitingTime();
			tempRelation.replyValue = reList.get(i).getReplyNum();
			rrList.add(tempRelation);
		}

		rrList = getSortRelation(rrList);

		ArrayList<RecommendationEntity> resultList = new ArrayList<RecommendationEntity>();
		for (int i = 0; i < rrList.size()
				&& i < topNum; i++)
			resultList.add(reList.get(rrList.get(i).index));
		return resultList;
		
	}

	/**
	 * 对输入数据的各个小项目统计，获取最大值，然后计算各项目单位值，在加权成最后值并排序返回
	 * @param rrList
	 * @return
	 */
	public synchronized static ArrayList<RecommendationRelation> getSortRelation(
			ArrayList<RecommendationRelation> rrList) {
		double maxClickFreq = 0, maxWaitingTime = 0, maxReplyNum = 0;
		//找出最大的点击次数，最大的停留时间，最大的回复次数
		for (int i = 0; i < rrList.size(); i++) {
			maxClickFreq = Math.max(rrList.get(i).clickValue, maxClickFreq);
			maxWaitingTime = Math.max(rrList.get(i).waitingValue,
					maxWaitingTime);
			maxReplyNum = Math.max(rrList.get(i).replyValue, maxReplyNum);
		}

		for (int i = 0; i < rrList.size(); i++) {
			RecommendationRelation tempRelation = rrList.get(i);
			tempRelation.index = i;
			tempRelation.clickValue = rrList.get(i).clickValue / maxClickFreq;
			tempRelation.waitingValue = rrList.get(i).waitingValue
					/ maxWaitingTime;
			tempRelation.replyValue = rrList.get(i).replyValue / maxReplyNum;
			tempRelation.value = SNSConstants.CLICK_PART
					* tempRelation.clickValue + SNSConstants.WAITING_PART
					* tempRelation.waitingValue + SNSConstants.REPLY_PART
					* tempRelation.replyValue;
		}
		
		//排序
		Collections.sort(rrList,
				new java.util.Comparator<RecommendationRelation>() {
					public int compare(RecommendationRelation o1,
							RecommendationRelation o2) {
						return (o1.value < o2.value) ? 1
								: ((o1.value > o2.value) ? -1 : 0);
					}
				});

		return rrList;
	}

}
